package com.virus.coronavirus.controller;

import com.virus.coronavirus.error.SystemException;
import com.virus.coronavirus.model.User;
import com.virus.coronavirus.model.vo.UserEditVO;
import com.virus.coronavirus.model.vo.UserInfoVO;
import com.virus.coronavirus.model.vo.UserNameVO;
import com.virus.coronavirus.model.vo.UserVO;
import com.virus.coronavirus.response.ControllerEndpoint;
import com.virus.coronavirus.response.ResponseBean;
import com.virus.coronavirus.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Bbeginner
 * @Date: 2021/2/4 16:44
 * @Version 1.0
 **/
@RestController
@RequestMapping("/virus/user")
@Validated
@Api(tags = "疫情防控模块-用户相关接口")
//@CrossOrigin(value = {"http://localhost:8081","http://localhost:8082"})
public class UserController {

    @Autowired
    private UserService userService;



    /**
     * 更新用户
     * @param id
     * @param userEditVO
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新用户失败", operation = "更新用户")
    @ApiOperation(value = "更新用户", notes = "更新用户信息")
    @RequiresPermissions({"user:update"})
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id, @RequestBody @Validated UserEditVO userEditVO) throws SystemException {
        userService.update(id, userEditVO);
        return ResponseBean.success();
    }

    /**
     * 添加用户信息
     * @param userVO
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加用户失败", operation = "添加用户")
    @ApiOperation(value = "添加用户", notes = "添加用户信息")
    @RequiresPermissions({"user:add"})
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated UserVO userVO) throws SystemException {
        userService.add(userVO);
        return ResponseBean.success();
    }

    /**
     * 删除用户
     *
     * @param id 用户ID
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除用户失败", operation = "删除用户")
    @RequiresPermissions({"user:delete"})
    @ApiOperation(value = "删除用户", notes = "删除用户信息，根据用户ID")
    @DeleteMapping("/delete/{id}")
//    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR,reason = "时间错误")
    public ResponseBean delete(@PathVariable Long id) throws SystemException {
        userService.deleteById(id);
        return ResponseBean.success();
    }

    /**
     * 编辑用户
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑用户", notes = "获取用户的详情，编辑用户信息")
    @RequiresPermissions({"user:edit"})
    @GetMapping("/edit/{id}")
    public ResponseBean<UserEditVO> edit(@PathVariable Long id) throws SystemException {
        UserEditVO userVO = userService.edit(id);
        return ResponseBean.success(userVO);
    }

    @ApiOperation(value = "通过名称查询用户", notes = "查询用户信息,根据用户名字")
    @GetMapping("/username/{name}")
    public ResponseBean<User> finduser(@PathVariable String name) throws SystemException {
        User user = userService.findUserByName(name);
        return ResponseBean.success(user);
    }

    @ApiOperation(value = "通过ID查询用户", notes = "查询用户信息,根据用户ID")
    @GetMapping("/userid/{id}")
    public ResponseBean<User> finduserbyid(@PathVariable Long id) throws SystemException {
//        userService.findUserById(id);
        User user = userService.findUserById(id);
        return ResponseBean.success(user);
    }

    @ApiOperation(value = "通过手机号查询用户", notes = "查询用户信息,根据用户手机号")
    @GetMapping("/userphone/{phone}")
    public ResponseBean<User> finduserbyphone(@PathVariable String phone) throws SystemException {
        User user = userService.findUserByPhone(phone);
        return ResponseBean.success(user);
    }

    //获得一个人所有的轨迹
    @ApiOperation(value = "通过状态查询用户", notes = "查询所有相同状态的用户")
    @GetMapping("/userstatus{status}")
    public ResponseBean findUserByStatus(@PathVariable Integer status){
        List<User> userByStatus = userService.findUserByStatus(status);
        return ResponseBean.success(userByStatus);
    }
    






    /*下面的功能暂时不去实现,因为需要出与安全考虑,用token和配置shiro*/

    /**
     * 用户部分信息
     *
     * @return
     */
    @ApiOperation(value = "所有用户的部分信息", notes = "用户登入信息")
    @GetMapping("/info")
    public ResponseBean info() throws SystemException {
        List<UserInfoVO> userInfoVO = userService.info();
        return ResponseBean.success(userInfoVO);
    }

    @ApiOperation(value = "所有的用户全部信息", notes = "用户登入信息")
    @GetMapping("/uinfo")
    public ResponseBean uinfo() throws SystemException {
        List<User> userallinfo = userService.userallinfo();
        return ResponseBean.success(userallinfo);
    }

    /**
     *
     * @param username
     * @return
     * @throws SystemException
     */
    @ApiOperation(value = "用户名登入", notes = "接收参数用户名和密码,登入成功后,返回JWTToken")
    @PostMapping("/login")
    public ResponseBean<String> login(@RequestBody UserNameVO username) throws SystemException {
        String token=userService.login(username);
//       loginLogService.add(request);
        return ResponseBean.success(token);
    }

    /**
     *
     * @param id
     * @param password
     * @return
     * @throws SystemException
     */
    @ApiOperation(value = "用户ID登入", notes = "接收参数ID和密码")
    @PostMapping("/loginid")
    public ResponseBean<String> loginid(@RequestBody Long id, String password) throws SystemException {
        String token=userService.loginid(id, password);
        return ResponseBean.success(token);
    }



}
